#include "cocoa/cnsview.h"
#include "cocoa/ccocoawindow.h"

@interface NSEvent (Qt_Compile_Leopard_DeviceDelta)
  - (CGFloat)deviceDeltaX;
  - (CGFloat)deviceDeltaY;
  - (CGFloat)deviceDeltaZ;
@end

@implementation CNSView

- (id)init:(buola::gui::cocoa::CCocoaWindow*)pWindow
{
    self = [super initWithFrame : NSMakeRect(0,0, 300,300)];
    if (!self) return 0;
    
    mWindow=pWindow;
//        m_cgImage = 0;
//        m_buttons = Qt::NoButton;
//        m_sendKeyEvent = false;
//        currentCustomDragTypes = 0;

//    [self registerDragTypes];
//    [self setPostsFrameChangedNotifications : YES];
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                          selector:@selector(updateGeometry)
//                                          name:NSViewFrameDidChangeNotification
//                                          object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                          selector:@selector(updateGeometry) 
                                          name:NSWindowDidResizeNotification
                                          object:[self window]];

    return self;
}

- (void)updateGeometry
{
    mWindow->DispatchGeometry();
//     NSRect rect = [self frame];
//     NSRect windowRect = [[self window] frame];
//     QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
// 
//     // Call setGeometry on QPlatformWindow. (not on QCocoaWindow,
//     // doing that will initiate a geometry change it and possibly create
//     // an infinite loop when this notification is triggered again.)
//     m_platformWindow->QPlatformWindow::setGeometry(geo);
// 
//     // Send a geometry change event to Qt, if it's ready to handle events
//     if (!m_platformWindow->m_inConstructor)
//         QWindowSystemInterface::handleSynchronousGeometryChange(m_window, geo);
}

- (void)windowNotification : (NSNotification *) windowNotification
{
//     //qDebug() << "windowNotification" << QCFString::toQString([windowNotification name]);
// 
//     NSString *notificationName = [windowNotification name];
//     if (notificationName == NSWindowDidBecomeKeyNotification) {
//         if (!m_platformWindow->windowIsPopupType())
//             QWindowSystemInterface::handleWindowActivated(m_window);
//     } else if (notificationName == NSWindowDidResignKeyNotification) {
//         // key window will be non-nil if another window became key... do not
//         // set the active window to zero here, the new key window's
//         // NSWindowDidBecomeKeyNotification hander will change the active window
//         NSWindow *keyWindow = [NSApp keyWindow];
//         if (!keyWindow) {
//             // no new key window, go ahead and set the active window to zero
//             if (!m_platformWindow->windowIsPopupType())
//                 QWindowSystemInterface::handleWindowActivated(0);
//         }
//     } else if (notificationName == NSWindowDidMiniaturizeNotification) {
//         QWindowSystemInterface::handleWindowStateChanged(m_window, Qt::WindowMinimized);
//     } else if (notificationName == NSWindowDidDeminiaturizeNotification) {
//         QWindowSystemInterface::handleWindowStateChanged(m_window, Qt::WindowNoState);
//         // Qt expects an expose event after restore/deminiaturize. This also needs
//         // to be a non-synchronous event to make sure it gets processed after
//         // the state change event sent above.
//         QWindowSystemInterface::handleExposeEvent(m_window, QRegion(m_window->geometry()));
//     } else {
// 
// #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
//     if (QSysInfo::QSysInfo::MacintoshVersion >= QSysInfo::MV_10_7) {
//         if (notificationName == NSWindowDidEnterFullScreenNotification) {
//             QWindowSystemInterface::handleWindowStateChanged(m_window, Qt::WindowFullScreen);
//         } else if (notificationName == NSWindowDidExitFullScreenNotification) {
//             QWindowSystemInterface::handleWindowStateChanged(m_window, Qt::WindowNoState);
//         }
//     }
// #endif
// 
//     }
}

- (void) drawRect:(NSRect)pRect
{
    buola::msg_info() << "paint event!!\n";
    mWindow->DispatchDrawRect(buola::gui::cocoa::nsrect_to_crect_d(pRect));

//     if (!m_cgImage)
//         return;
// 
//     CGRect dirtyCGRect = NSRectToCGRect(dirtyRect);
// 
//     NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext currentContext];
//     CGContextRef cgContext = (CGContextRef) [nsGraphicsContext graphicsPort];
// 
//     CGContextSaveGState( cgContext );
//     int dy = dirtyCGRect.origin.y + CGRectGetMaxY(dirtyCGRect);
//     CGContextTranslateCTM(cgContext, 0, dy);
//     CGContextScaleCTM(cgContext, 1, -1);
// 
//     CGImageRef subImage = CGImageCreateWithImageInRect(m_cgImage, dirtyCGRect);
//     CGContextDrawImage(cgContext,dirtyCGRect,subImage);
// 
//     CGContextRestoreGState(cgContext);
// 
//     CGImageRelease(subImage);
// 
}

- (BOOL) isFlipped
{
    return YES;
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
    return YES;
}

- (void)handleFrameStrutMouseEvent:(NSEvent *)theEvent
{
//     // get m_buttons in sync
//     NSEventType ty = [theEvent type];
//     switch (ty) {
//     case NSLeftMouseDown:
//         m_buttons |= Qt::LeftButton;
//         break;
//     case NSLeftMouseUp:
//          m_buttons &= QFlag(~int(Qt::LeftButton));
//          break;
//     case NSRightMouseDown:
//         m_buttons |= Qt::RightButton;
//         break;
//     case NSRightMouseUp:
//         m_buttons &= QFlag(~int(Qt::RightButton));
//         break;
//     default:
//         break;
//     }
// 
//     NSWindow *window = [self window];
//     int windowHeight = [window frame].size.height;
//     NSPoint windowPoint = [theEvent locationInWindow];
//     NSPoint nsViewPoint = [self convertPoint: windowPoint fromView: nil];
//     QPoint qtWindowPoint = QPoint(nsViewPoint.x, windowHeight - nsViewPoint.y);
//     NSPoint screenPoint = [window convertBaseToScreen : windowPoint];
//     QPoint qtScreenPoint = QPoint(screenPoint.x, qt_mac_flipYCoordinate(screenPoint.y));
// 
//     ulong timestamp = [theEvent timestamp] * 1000;
//     QWindowSystemInterface::handleFrameStrutMouseEvent(m_window, timestamp, qtWindowPoint, qtScreenPoint, m_buttons);
}

- (void)mouseDown:(NSEvent *)pEvent
{
    buola::msg_info() << "mouse down\n";
    mWindow->DispatchButton(buola::gui::CMouseEvent::PRESS,buola::gui::EMouseButton::LEFT,pEvent);

//     if (m_platformWindow->m_activePopupWindow) {
//         QWindowSystemInterface::handleSynchronousCloseEvent(m_platformWindow->m_activePopupWindow);
//         m_platformWindow->m_activePopupWindow = 0;
//     }
//     if ([self hasMarkedText]) {
//         NSInputManager* inputManager = [NSInputManager currentInputManager];
//         if ([inputManager wantsToHandleMouseEvents]) {
//             [inputManager handleMouseEvent:theEvent];
//         }
//     } else {
//         m_buttons |= Qt::LeftButton;
//         [self handleMouseEvent:theEvent];
//     }
}

- (void)mouseDragged:(NSEvent *)pEvent
{
    mWindow->DispatchMotion(pEvent);
}

- (void)mouseUp:(NSEvent *)pEvent
{
    mWindow->DispatchButton(buola::gui::CMouseEvent::RELEASE,buola::gui::EMouseButton::LEFT,pEvent);

//     m_buttons &= QFlag(~int(Qt::LeftButton));
}

- (void)mouseMoved:(NSEvent *)pEvent
{
    mWindow->DispatchMotion(pEvent);
}

- (void)mouseEntered:(NSEvent *)theEvent
{
    buola::msg_info() << "mouse entered\n";
//     Q_UNUSED(theEvent);
//     QWindowSystemInterface::handleEnterEvent(m_window);
}

- (void)mouseExited:(NSEvent *)theEvent
{
    buola::msg_info() << "mouse exited\n";
//     Q_UNUSED(theEvent);
//     QWindowSystemInterface::handleLeaveEvent(m_window);
}

- (void)rightMouseDown:(NSEvent *)pEvent
{
    mWindow->DispatchButton(buola::gui::CMouseEvent::PRESS,buola::gui::EMouseButton::RIGHT,pEvent);
//     m_buttons |= Qt::RightButton;
}

- (void)rightMouseDragged:(NSEvent *)pEvent
{
    mWindow->DispatchMotion(pEvent);
}

- (void)rightMouseUp:(NSEvent *)pEvent
{
    mWindow->DispatchButton(buola::gui::CMouseEvent::RELEASE,buola::gui::EMouseButton::RIGHT,pEvent);
//     m_buttons &= QFlag(~int(Qt::RightButton));
}

- (void)otherMouseDown:(NSEvent *)theEvent
{
    buola::msg_info() << "mouse other down\n";

//     switch ([theEvent buttonNumber]) {
//         case 3:
//             m_buttons |= Qt::MiddleButton;
//             break;
//         case 4:
//             m_buttons |= Qt::ExtraButton1;  // AKA Qt::BackButton
//             break;
//         case 5:
//             m_buttons |= Qt::ExtraButton2;  // AKA Qt::ForwardButton
//             break;
//         case 6:
//             m_buttons |= Qt::ExtraButton3;
//             break;
//         case 7:
//             m_buttons |= Qt::ExtraButton4;
//             break;
//         case 8:
//             m_buttons |= Qt::ExtraButton5;
//             break;
//         case 9:
//             m_buttons |= Qt::ExtraButton6;
//             break;
//         case 10:
//             m_buttons |= Qt::ExtraButton7;
//             break;
//         case 11:
//             m_buttons |= Qt::ExtraButton8;
//             break;
//         case 12:
//             m_buttons |= Qt::ExtraButton9;
//             break;
//         case 13:
//             m_buttons |= Qt::ExtraButton10;
//             break;
//         case 14:
//             m_buttons |= Qt::ExtraButton11;
//             break;
//         case 15:
//             m_buttons |= Qt::ExtraButton12;
//             break;
//         case 16:
//             m_buttons |= Qt::ExtraButton13;
//             break;
//         default:
//             m_buttons |= Qt::MiddleButton;
//             break;
//     }
//     [self handleMouseEvent:theEvent];
}

- (void)otherMouseDragged:(NSEvent *)theEvent
{
//     if (!(m_buttons & ~(Qt::LeftButton | Qt::RightButton)))
//         qWarning("QNSView otherMouseDragged: Internal mouse button tracking invalid (missing Qt::MiddleButton or Qt::ExtraButton*)");
//     [self handleMouseEvent:theEvent];
}

- (void)otherMouseUp:(NSEvent *)theEvent
{
//     switch ([theEvent buttonNumber]) {
//         case 3:
//             m_buttons &= QFlag(~int(Qt::MiddleButton));
//             break;
//         case 4:
//             m_buttons &= QFlag(~int(Qt::ExtraButton1));  // AKA Qt::BackButton
//             break;
//         case 5:
//             m_buttons &= QFlag(~int(Qt::ExtraButton2));  // AKA Qt::ForwardButton
//             break;
//         case 6:
//             m_buttons &= QFlag(~int(Qt::ExtraButton3));
//             break;
//         case 7:
//             m_buttons &= QFlag(~int(Qt::ExtraButton4));
//             break;
//         case 8:
//             m_buttons &= QFlag(~int(Qt::ExtraButton5));
//             break;
//         case 9:
//             m_buttons &= QFlag(~int(Qt::ExtraButton6));
//             break;
//         case 10:
//             m_buttons &= QFlag(~int(Qt::ExtraButton7));
//             break;
//         case 11:
//             m_buttons &= QFlag(~int(Qt::ExtraButton8));
//             break;
//         case 12:
//             m_buttons &= QFlag(~int(Qt::ExtraButton9));
//             break;
//         case 13:
//             m_buttons &= QFlag(~int(Qt::ExtraButton10));
//             break;
//         case 14:
//             m_buttons &= QFlag(~int(Qt::ExtraButton11));
//             break;
//         case 15:
//             m_buttons &= QFlag(~int(Qt::ExtraButton12));
//             break;
//         case 16:
//             m_buttons &= QFlag(~int(Qt::ExtraButton13));
//             break;
//         default:
//             m_buttons &= QFlag(~int(Qt::MiddleButton));
//             break;
//     }
//     [self handleMouseEvent:theEvent];
}

- (void)touchesBeganWithEvent:(NSEvent *)event
{
//     const NSTimeInterval timestamp = [event timestamp];
//     const QList<QWindowSystemInterface::TouchPoint> points = QCocoaTouch::getCurrentTouchPointList(event, /*acceptSingleTouch= ### true or false?*/false);
//     QWindowSystemInterface::handleTouchEvent(m_window, timestamp * 1000, touchDevice, points);
}

- (void)touchesMovedWithEvent:(NSEvent *)event
{
//     const NSTimeInterval timestamp = [event timestamp];
//     const QList<QWindowSystemInterface::TouchPoint> points = QCocoaTouch::getCurrentTouchPointList(event, /*acceptSingleTouch= ### true or false?*/false);
//     QWindowSystemInterface::handleTouchEvent(m_window, timestamp * 1000, touchDevice, points);
}

- (void)touchesEndedWithEvent:(NSEvent *)event
{
//     const NSTimeInterval timestamp = [event timestamp];
//     const QList<QWindowSystemInterface::TouchPoint> points = QCocoaTouch::getCurrentTouchPointList(event, /*acceptSingleTouch= ### true or false?*/false);
//     QWindowSystemInterface::handleTouchEvent(m_window, timestamp * 1000, touchDevice, points);
}

- (void)touchesCancelledWithEvent:(NSEvent *)event
{
//     const NSTimeInterval timestamp = [event timestamp];
//     const QList<QWindowSystemInterface::TouchPoint> points = QCocoaTouch::getCurrentTouchPointList(event, /*acceptSingleTouch= ### true or false?*/false);
//     QWindowSystemInterface::handleTouchEvent(m_window, timestamp * 1000, touchDevice, points);
}

- (void)scrollWheel:(NSEvent *)pEvent
{
    mWindow->DispatchWheel(pEvent);
}

// - (int) convertKeyCode : (QChar)keyChar
// {
//     return qt_mac_cocoaKey2QtKey(keyChar);
// }
// 

- (void)keyDown:(NSEvent*)pEvent
{
    mWindow->DispatchKey(buola::gui::CKeyEvent::PRESS,pEvent);
}

- (void)keyUp:(NSEvent *)pEvent
{
    mWindow->DispatchKey(buola::gui::CKeyEvent::RELEASE,pEvent);
}

- (void)flagsChanged:(NSEvent*)pEvent
{
    mWindow->DispatchModifier(pEvent);
}

- (void) doCommandBySelector:(SEL)aSelector
{
//     [self tryToPerform:aSelector with:self];
}

- (void) insertText:(id)aString replacementRange:(NSRange)replacementRange
{
//     Q_UNUSED(replacementRange)
// 
//     if (m_sendKeyEvent && m_composingText.isEmpty()) {
//         // don't send input method events for simple text input (let handleKeyEvent send key events instead)
//         return;
//     }
// 
//     QString commitString;
//     if ([aString length]) {
//         if ([aString isKindOfClass:[NSAttributedString class]]) {
//             commitString = QCFString::toQString(reinterpret_cast<CFStringRef>([aString string]));
//         } else {
//             commitString = QCFString::toQString(reinterpret_cast<CFStringRef>(aString));
//         };
//     }
//     QObject *fo = QGuiApplication::focusObject();
//     if (fo) {
//         QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
//         if (QCoreApplication::sendEvent(fo, &queryEvent)) {
//             if (queryEvent.value(Qt::ImEnabled).toBool()) {
//                 QInputMethodEvent e;
//                 e.setCommitString(commitString);
//                 QCoreApplication::sendEvent(fo, &e);
//                 // prevent handleKeyEvent from sending a key event
//                 m_sendKeyEvent = false;
//             }
//         }
//     }
// 
//     m_composingText.clear();
}

- (void) setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange
{
//     Q_UNUSED(replacementRange)
//     QString preeditString;
// 
//     QList<QInputMethodEvent::Attribute> attrs;
//     attrs<<QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, selectedRange.location + selectedRange.length, 1, QVariant());
// 
//     if ([aString isKindOfClass:[NSAttributedString class]]) {
//         // Preedit string has attribution
//         preeditString = QCFString::toQString(reinterpret_cast<CFStringRef>([aString string]));
//         int composingLength = preeditString.length();
//         int index = 0;
//         // Create attributes for individual sections of preedit text
//         while (index < composingLength) {
//             NSRange effectiveRange;
//             NSRange range = NSMakeRange(index, composingLength-index);
//             NSDictionary *attributes = [aString attributesAtIndex:index
//                                             longestEffectiveRange:&effectiveRange
//                                                           inRange:range];
//             NSNumber *underlineStyle = [attributes objectForKey:NSUnderlineStyleAttributeName];
//             if (underlineStyle) {
//                 QColor clr (Qt::black);
//                 NSColor *color = [attributes objectForKey:NSUnderlineColorAttributeName];
//                 if (color) {
//                     clr = qt_mac_toQColor(color);
//                 }
//                 QTextCharFormat format;
//                 format.setFontUnderline(true);
//                 format.setUnderlineColor(clr);
//                 attrs<<QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
//                                                     effectiveRange.location,
//                                                     effectiveRange.length,
//                                                     format);
//             }
//             index = effectiveRange.location + effectiveRange.length;
//         }
//     } else {
//         // No attributes specified, take only the preedit text.
//         preeditString = QCFString::toQString(reinterpret_cast<CFStringRef>(aString));
//     }
// 
//     if (attrs.isEmpty()) {
//         QTextCharFormat format;
//         format.setFontUnderline(true);
//         attrs<<QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
//                                             0, preeditString.length(), format);
//     }
// 
//     m_composingText = preeditString;
// 
//     QObject *fo = QGuiApplication::focusObject();
//     if (fo) {
//         QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
//         if (QCoreApplication::sendEvent(fo, &queryEvent)) {
//             if (queryEvent.value(Qt::ImEnabled).toBool()) {
//                 QInputMethodEvent e(preeditString, attrs);
//                 QCoreApplication::sendEvent(fo, &e);
//                 // prevent handleKeyEvent from sending a key event
//                 m_sendKeyEvent = false;
//             }
//         }
//     }
}

- (void) unmarkText
{
//     if (!m_composingText.isEmpty()) {
//         QObject *fo = QGuiApplication::focusObject();
//         if (fo) {
//             QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
//             if (QCoreApplication::sendEvent(fo, &queryEvent)) {
//                 if (queryEvent.value(Qt::ImEnabled).toBool()) {
//                     QInputMethodEvent e;
//                     e.setCommitString(m_composingText);
//                     QCoreApplication::sendEvent(fo, &e);
//                 }
//             }
//         }
//     }
//     m_composingText.clear();
}

- (BOOL) hasMarkedText
{
//     return (m_composingText.isEmpty() ? NO: YES);
    return NO;
}

- (NSAttributedString *) attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange
{
//     Q_UNUSED(actualRange)
//     QObject *fo = QGuiApplication::focusObject();
//     if (!fo)
//         return nil;
//     QInputMethodQueryEvent queryEvent(Qt::ImEnabled | Qt::ImCurrentSelection);
//     if (!QCoreApplication::sendEvent(fo, &queryEvent))
//         return nil;
//     if (!queryEvent.value(Qt::ImEnabled).toBool())
//         return nil;
// 
//     QString selectedText = queryEvent.value(Qt::ImCurrentSelection).toString();
//     if (selectedText.isEmpty())
//         return nil;
// 
//     QCFString string(selectedText.mid(aRange.location, aRange.length));
//     const NSString *tmpString = reinterpret_cast<const NSString *>((CFStringRef)string);
//     return [[[NSAttributedString alloc]  initWithString:const_cast<NSString *>(tmpString)] autorelease];
    return 0;
}

- (NSRange) markedRange
{
     NSRange range;
//     if (!m_composingText.isEmpty()) {
//         range.location = 0;
//         range.length = m_composingText.length();
//     } else {
//         range.location = NSNotFound;
//         range.length = 0;
//     }
     return range;
}

- (NSRange) selectedRange
{
     NSRange selectedRange = {NSNotFound, 0};
//     selectedRange.location = NSNotFound;
//     selectedRange.length = 0;
// 
//     QObject *fo = QGuiApplication::focusObject();
//     if (!fo)
//         return selectedRange;
//     QInputMethodQueryEvent queryEvent(Qt::ImEnabled | Qt::ImCurrentSelection);
//     if (!QCoreApplication::sendEvent(fo, &queryEvent))
//         return selectedRange;
//     if (!queryEvent.value(Qt::ImEnabled).toBool())
//         return selectedRange;
// 
//     QString selectedText = queryEvent.value(Qt::ImCurrentSelection).toString();
// 
//     if (!selectedText.isEmpty()) {
//         selectedRange.location = 0;
//         selectedRange.length = selectedText.length();
//     }
     return selectedRange;
}

- (NSRect) firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange
{
//     Q_UNUSED(aRange)
//     Q_UNUSED(actualRange)
//     QObject *fo = QGuiApplication::focusObject();
//     if (!fo)
//         return NSZeroRect;
// 
//     QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
//     if (!QCoreApplication::sendEvent(fo, &queryEvent))
//         return NSZeroRect;
//     if (!queryEvent.value(Qt::ImEnabled).toBool())
//         return NSZeroRect;
// 
//     if (!m_window)
//         return NSZeroRect;
// 
//     // The returned rect is always based on the internal cursor.
//     QRect mr = qApp->inputMethod()->cursorRectangle().toRect();
//     QPoint mp = m_window->mapToGlobal(mr.bottomLeft());
// 
     NSRect rect;
//     rect.origin.x = mp.x();
//     rect.origin.y = qt_mac_flipYCoordinate(mp.y());
//     rect.size.width = mr.width();
//     rect.size.height = mr.height();
     return rect;
}

- (NSUInteger)characterIndexForPoint:(NSPoint)aPoint
{
//     // We dont support cursor movements using mouse while composing.
//     Q_UNUSED(aPoint);
     return NSNotFound;
}

- (NSArray*) validAttributesForMarkedText
{
//     if (m_window != QGuiApplication::focusWindow())
//         return nil;
// 
//     QObject *fo = QGuiApplication::focusObject();
//     if (!fo)
//         return nil;
// 
//     QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
//     if (!QCoreApplication::sendEvent(fo, &queryEvent))
//         return nil;
//     if (!queryEvent.value(Qt::ImEnabled).toBool())
//         return nil;
// 
//     // Support only underline color/style.
//     return [NSArray arrayWithObjects:NSUnderlineColorAttributeName,
//                                      NSUnderlineStyleAttributeName, nil];
    return 0;
}

-(void)registerDragTypes
{
//     QCocoaAutoReleasePool pool;
//     // ### Custom types disabled.
//     QStringList customTypes;  // = qEnabledDraggedTypes();
//     if (currentCustomDragTypes == 0 || *currentCustomDragTypes != customTypes) {
//         if (currentCustomDragTypes == 0)
//             currentCustomDragTypes = new QStringList();
//         *currentCustomDragTypes = customTypes;
//         const NSString* mimeTypeGeneric = @"com.trolltech.qt.MimeTypeName";
//         NSMutableArray *supportedTypes = [NSMutableArray arrayWithObjects:NSColorPboardType,
//                        NSFilenamesPboardType, NSStringPboardType,
//                        NSFilenamesPboardType, NSPostScriptPboardType, NSTIFFPboardType,
//                        NSRTFPboardType, NSTabularTextPboardType, NSFontPboardType,
//                        NSRulerPboardType, NSFileContentsPboardType, NSColorPboardType,
//                        NSRTFDPboardType, NSHTMLPboardType, NSPICTPboardType,
//                        NSURLPboardType, NSPDFPboardType, NSVCardPboardType,
//                        NSFilesPromisePboardType, NSInkTextPboardType,
//                        NSMultipleTextSelectionPboardType, mimeTypeGeneric, nil];
//         // Add custom types supported by the application.
//         for (int i = 0; i < customTypes.size(); i++) {
//            [supportedTypes addObject:QCFString::toNSString(customTypes[i])];
//         }
//         [self registerForDraggedTypes:supportedTypes];
//     }
}

- (NSDragOperation) draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
//     Q_UNUSED(isLocal);
//     QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag());
//     return qt_mac_mapDropActions(nativeDrag->currentDrag()->supportedActions());
    return 0;
}

- (BOOL) ignoreModifierKeysWhileDragging
{
    return NO;
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    return [self handleDrag : sender];
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
    return [self handleDrag : sender];
}

// Sends drag update to Qt, return the action
- (NSDragOperation)handleDrag:(id <NSDraggingInfo>)sender
{
//     NSPoint windowPoint = [self convertPoint: [sender draggingLocation] fromView: nil];
//     QPoint qt_windowPoint(windowPoint.x, windowPoint.y);
//     Qt::DropActions qtAllowed = qt_mac_mapNSDragOperations([sender draggingSourceOperationMask]);
// 
//     // update these so selecting move/copy/link works
//     QGuiApplicationPrivate::modifier_buttons = [self convertKeyModifiers: [[NSApp currentEvent] modifierFlags]];
// 
//     QPlatformDragQtResponse response(false, Qt::IgnoreAction, QRect());
//     if ([sender draggingSource] != nil) {
//         QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag());
//         response = QWindowSystemInterface::handleDrag(m_window, nativeDrag->platformDropData(), qt_windowPoint, qtAllowed);
//     } else {
//         QCocoaDropData mimeData([sender draggingPasteboard]);
//         response = QWindowSystemInterface::handleDrag(m_window, &mimeData, qt_windowPoint, qtAllowed);
//     }
// 
//     return qt_mac_mapDropAction(response.acceptedAction());
    return 0;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
//     NSPoint windowPoint = [self convertPoint: [sender draggingLocation] fromView: nil];
//     QPoint qt_windowPoint(windowPoint.x, windowPoint.y);
// 
//     // Send 0 mime data to indicate drag exit
//     QWindowSystemInterface::handleDrag(m_window, 0 ,qt_windowPoint, Qt::IgnoreAction);
}

// called on drop, send the drop to Qt and return if it was accepted.
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
//     NSPoint windowPoint = [self convertPoint: [sender draggingLocation] fromView: nil];
//     QPoint qt_windowPoint(windowPoint.x, windowPoint.y);
//     Qt::DropActions qtAllowed = qt_mac_mapNSDragOperations([sender draggingSourceOperationMask]);
// 
//     QPlatformDropQtResponse response(false, Qt::IgnoreAction);
//     if ([sender draggingSource] != nil) {
//         QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag());
//         response = QWindowSystemInterface::handleDrop(m_window, nativeDrag->platformDropData(), qt_windowPoint, qtAllowed);
//     } else {
//         QCocoaDropData mimeData([sender draggingPasteboard]);
//         response = QWindowSystemInterface::handleDrop(m_window, &mimeData, qt_windowPoint, qtAllowed);
//     }
//     return response.isAccepted();
    return NO;
}

- (void)draggedImage:(NSImage*) img endedAt:(NSPoint) point operation:(NSDragOperation) operation
{
//     Q_UNUSED(img);
//     QCocoaDrag* nativeDrag = static_cast<QCocoaDrag *>(QGuiApplicationPrivate::platformIntegration()->drag());
//     nativeDrag->setAcceptedAction(qt_mac_mapNSDragOperation(operation));
// 
// // keep our state, and QGuiApplication state (buttons member) in-sync,
// // or future mouse events will be processed incorrectly
//     m_buttons &= QFlag(~int(Qt::LeftButton));
// 
//     NSPoint windowPoint = [self convertPoint: point fromView: nil];
//     QPoint qtWindowPoint(windowPoint.x, windowPoint.y);
// 
//     NSWindow *window = [self window];
//     NSPoint screenPoint = [window convertBaseToScreen :point];
//     QPoint qtScreenPoint = QPoint(screenPoint.x, qt_mac_flipYCoordinate(screenPoint.y));
// 
//     QWindowSystemInterface::handleMouseEvent(m_window, qtWindowPoint, qtScreenPoint, m_buttons);
}

@end
